package com.yywq.utils;

import com.yywq.error.BusinessException;
import com.yywq.error.EnumBusinessError;
import com.yywq.pojo.model.UserToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author: 吴磊
 * @program: yywq
 * @create: 2021-03-20 23:30
 */
@Component
public class MyUtil {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private TokenUtils tokenUtils;

    /**
     * 返回一个UUID
     *
     * @return
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replace('-', 'w');
    }

    /**
     * 返回一个格式化好的时间
     *
     * @return
     */
    public static String getCurrentTime() {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    /**
     * 判断验证码是否正确
     *
     * @param phone
     * @param verification
     * @return
     */
    public boolean verificationIsOk(String phone, String verification) {
        String phone_ver = (String) redisUtil.get(phone);
        if (phone_ver == null) {
            return false;
        }
        return phone_ver.equals(verification);
    }

    /**
     * 存储Token
     *
     * @param token
     * @param telPhone
     * @return
     * @throws BusinessException
     */
    public boolean Token(String token, String telPhone) throws BusinessException {
        try {
            redisUtil.set(token, telPhone, 60 * 24);
            return true;
        } catch (Exception e) {
            throw new BusinessException(EnumBusinessError.UNKNOWN_ERROR, "存储令牌失败");
        }
    }

    /**
     * 检查token是否生效
     *
     * @param token
     * @return
     */
    public boolean inspectionToken(String token) throws BusinessException {
        try {
            boolean b = redisUtil.hasKey(token);
            if (b) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            throw new BusinessException(EnumBusinessError.USER_NOT_LOGIN, "请登录后再执行此操作");
        }
    }

    /**
     * 当用户主动退出登录或者关闭浏览器的时候，移除用户token
     *
     * @param token
     * @return
     * @throws BusinessException
     */
    public boolean removeToken(String token) throws BusinessException {
        try {
            redisUtil.del(token);
        } catch (Exception e) {
            throw new BusinessException(EnumBusinessError.UNKNOWN_ERROR, "移除Token失败，请稍后再试");
        }
        return true;
    }

    /**
     * 获取当前用户的信息
     *
     * @return
     * @throws Exception
     */
    public UserToken getUserToken() throws Exception {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        String token = request.getHeader("Authorization");
        //通过Token获取买家信息
        UserToken userToken = tokenUtils.getInfoFromToken(token);
        return userToken;
    }

}
